<template>
  <!-- 工程师好难 -->
  <div class="cart">
    <div class="good-list" v-if="list.length > 0">
      <div class="goods" v-for="data in list" :key="data._id">
        <van-checkbox class="checkbox" v-model="data.checked"
          >复选框</van-checkbox
        >
        <img class="goods-img" :src="data.product.coverImg" alt="" />
        <div class="goods-info">
          <p>{{ data.product.name }}</p>
          <span>￥{{ data.product.price }}</span>
          <span @click="updatePro(data.product._id, 1)">+</span>
          {{ data.quantity }}
          <span @click="data.quantity > 1 && updatePro(data.product._id, -1)"
            >-</span
          >
      <van-icon name="delete-o" v-show="data.checked" @click="del(data._id)" />

        </div>
      </div>
    </div>
    <div v-else>购物车空空如也</div>
    <!-- 全选 -->
    <van-submit-bar
      :price="sumPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
     
    >
      <van-checkbox class="checkbox" v-model="checked" />
      <span>全选</span>

      <van-icon name="delete-o" @click="dels(dels)" />
    </van-submit-bar>
  </div>
</template>

<script>

import { del, set } from 'vue';
import {addtocart, reqCartlist, reqDeleteCart,reqDeleteCarts} from '../../api/cart'
import { get } from '@/util/request';
import router from '@/router';
export default {
  data() {
    return {
      list:[],
      // tijiao='提交成功'
    };
    
  },
    
    //计算属性
    // $set vue中添加动态属性
  computed: {
    // test(){
    //   return 123
    // },
    //无论怎么修改set方法最后走的一定是get方法
    checked:{
      set(flag){
        this.list.map(item=>this.$set(item,"checked",flag))
      },
      get(){
        //购物车的长度删除完了，变成0了
        if(this.list.length==0){
          return false
        }else{
          return(
            this.list.length===this.list.filter((item)=>item.checked).length
          )
        }
      },
      
    },

    //返回选中项
    selectgoods(){
      let selectList=[]
      this.list.filter(item=>{
        if(item.checked){
          selectList.push(item._id)
        }
      })
      return selectList
    },

    //计算总价
    sumPrice(){
      //reduce来实现求和
      return this.list.filter((item)=>{
        return item.checked
      }).reduce(function(pre,cur){
        return pre+=cur.product.price*cur.quantity
      },0)
    }
  },
  watch: {},

  methods: {
    // reqCartlist,
   async initCart(){
      const result=await reqCartlist()
      console.log(result)
      this.list=result
    },
    //添加减少购物车
    async updatePro(id,num=1){
      const result=await addtocart({
        product:id,
        quantity:num
      })
      console.log("121212",result)
      // 下面的操作一定要发生在添加或者删除成功之后的回调中
      // 客户端操作
       this.list.forEach((data) => {
        if (data.product._id == id) {
          data.quantity = data.quantity + num;
        }
      });
    },

    // async del(id){
    //   const result=await reqDeleteCart(id)
    //   console.log("删除",result)
    // },

    //根据id删除单个购物车数据
    async del(id){
      const result=await reqDeleteCart(id)
      console.log("删除",result)
    },
    //批量删除
    async dels(){
      // ids数据从哪来呢
      const result=await reqDeleteCarts(this.selectgoods)
      console.log(66666,result)
    },
    //提交订单
    onSubmit(){
      return true
      const tanchu= alert("提交成功")
    }


  },
  created() {
    this.initCart()
  },
  mounted() {},
  components: {},
};
</script>
<style scoped>
.good-list {
  padding-bottom: 50px;
}
.goods {
  display: flex;
  height: 100px;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 15px;
}
.checkbox {
  width: 20px;
  height: 20px;
}
.goods-img {
  width: 80px;
  height: 80px;
}
.goods-info {
  flex: 1;
  padding-left: 10px;
}
</style>
